﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Orchard.Test.Library;
using Selenium;

namespace Orchard.Test.Modules.Widgets.Auto
{
    [TestClass]
    public class AddLayerWithInvalidRulesUnderIronRubyScriptingDLR
    {
        private TestLibrary TestLibrary;
        private ISelenium selenium;

        [TestInitialize]
        public void SetupTest()
        {
            TestLibrary = TestLibrary.SetupTest(this.ToString());
            selenium = TestLibrary.Selenium;
        }

        [TestCleanup]
        public void TeardownTest()
        {
            TestLibrary.ShutDown();
        }

        [TestMethod]
        public void Widget_AddLayerWithInvalidRulesUnderIronRubyScriptingDLR()
        {
            string errorMessage = "";         
            string validPartMessage = "The rule is not valid: ";

            try
            {
                TestLibrary.UserHelper.LogOnAsAdmin();
                TestLibrary.ModuleHelper.EnableScriptingDLR();
                TestLibrary.ModuleHelper.DisableLightWeightScripting();

                #region Add layer with invalid rule
                
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer0", TestLibrary.Consts.AdminUserName, "This is layer00", " url ('~/default*', '~/home*') && 123>122"); // this rule is valid under "LightWeightScripting"(default scripting engin)
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected `,&#39"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer1", TestLibrary.Consts.AdminUserName, "This is layer01", "url ('~/default*') & authenticated");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "undefined method `&amp;&#39; for &quot;~/default*&quot;:String"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer2", TestLibrary.Consts.AdminUserName, "This is layer02", "url ('~/default*') && 123=123");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected `=&#39;"));
               
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer3", TestLibrary.Consts.AdminUserName, "This is layer03", "url ('~/default*') && a<=b");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "undefined method `&lt;=&#39; for nil:NilClass"));

                TestLibrary.WidgetHelper.AddWidgetLayer("Layer5", TestLibrary.Consts.AdminUserName, "This is layer05", "url ('~/default*') && @>$");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected `@&#39;"));

                TestLibrary.WidgetHelper.AddWidgetLayer("Layer8", TestLibrary.Consts.AdminUserName, "This is layer08", "url ('~/default*') && 10==10/0");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Attempted to divide by zero"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer11", TestLibrary.Consts.AdminUserName, "This is layer11", "url ('~/default*') && (10>1)/123");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "undefined method `/&#39; for true:TrueClass"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer13", TestLibrary.Consts.AdminUserName, "This is layer13", "url ('~/default*') && 3>false");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "comparison of Fixnum with FalseClass failed"));                
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer14", TestLibrary.Consts.AdminUserName, "This is layer14", "eval (\"1\"+\"1\") >1");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "comparison of String with Fixnum failed"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer15", TestLibrary.Consts.AdminUserName, "This is layer15", " url ('~/default*') && ");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected `}&#39;"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer16", TestLibrary.Consts.AdminUserName, "This is layer16", " url ('~/default*') && >");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected `&gt;&#39;"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer17", TestLibrary.Consts.AdminUserName, "This is layer17", "url ('~/default*') &&  null>123");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "undefined method `&gt;&#39; for nil:NilClass"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer19", TestLibrary.Consts.AdminUserName, "This is layer19", "url ('~/default*') &&  not null");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected identifier, expecting `(&#39;"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer21", TestLibrary.Consts.AdminUserName, "This is layer21", "ur('~/default*') && true");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Expression is not a boolean value"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer22", TestLibrary.Consts.AdminUserName, "This is layer22", "url ('~/default*') &&  1/( )");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "NilClass can&#39;t be coerced into Fixnum"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer23", TestLibrary.Consts.AdminUserName, "This is layer23", "true&3>2");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "undefined method `&gt;&#39; for true:TrueClass"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer23", TestLibrary.Consts.AdminUserName, "This is layer23", "true'&'3>2");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected quote"));

                TestLibrary.WidgetHelper.AddWidgetLayer("Layer24", TestLibrary.Consts.AdminUserName, "This is layer24", "url ('~/default*') && 'bad' =~ /a/<2");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "undefined method `&lt;&#39; for /a/:Regexp"));

                TestLibrary.WidgetHelper.AddWidgetLayer("Layer25", TestLibrary.Consts.AdminUserName, "This is layer25", "url=~/'~/default*'/");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Unknown RegEx option &#39;d&#39;"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer26", TestLibrary.Consts.AdminUserName, "This is layer26", "url=~ /'~\\/default'[0-9]/");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Index was outside the bounds of the array"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer27", TestLibrary.Consts.AdminUserName, "This is layer27", "url('~/default'+/[a-z]+[0-9]/)");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "can&#39;t convert Regexp into String"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer28", TestLibrary.Consts.AdminUserName, "This is layer28", "url (~/default/)");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "can&#39;t convert nil into String"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer029", TestLibrary.Consts.AdminUserName, "This is layer29", "url ('~/default*') && '\\'=='\\'");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected `\\&#39;"));
                //use '=' assignment statement
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer030", TestLibrary.Consts.AdminUserName, "This is layer30", " (i=1+1)==(j=2-0) && i");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Expression is not a boolean value"));
                //use 'def', 'return' reserved keywords
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer031", TestLibrary.Consts.AdminUserName, "This is layer31", " def f\r\nreturn 32\r\n f/4==8");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected `}&#39;, expecting `rescue&#39;, `else&#39;, `ensure&#39;, or `end&#39;"));
         
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer032", TestLibrary.Consts.AdminUserName, "This is layer32", " def f\r\nreturn 32\r\n f/4");
                //Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Unterminated string meets end of file"));   //will throw the errorMsg same as previous one ,can note repro this message, don't know why
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer033", TestLibrary.Consts.AdminUserName, "This is layer33", " (return 32) /4==8");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "unexpected return"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer034", TestLibrary.Consts.AdminUserName, "This is layer34", " return  \"hello\" + \"world\" ");
                Assert.IsTrue((selenium.IsTextPresent(validPartMessage + "can&#39;t convert NilClass into String") || selenium.IsTextPresent(validPartMessage + "unexpected return")));                
                
                //execute ruby '.rb' file
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer035", TestLibrary.Consts.AdminUserName, "This is layer35", " (f=c:\\testRuby.rb)/4==8");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected label"));

                TestLibrary.WidgetHelper.AddWidgetLayer("Layer036", TestLibrary.Consts.AdminUserName, "This is layer36", " 1+1 equal to 2");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "syntax error, unexpected identifier"));
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer037", TestLibrary.Consts.AdminUserName, "This is layer37", " def f=ExecuteFile(\"C:\\testRuby.rb\")");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Formal argument cannot be a constant"));
                //with IronRuby module
                 TestLibrary.WidgetHelper.AddWidgetLayer("Layer038", TestLibrary.Consts.AdminUserName, "This is layer38", " IronRuby.Ruby.CreateEngine()..ExecuteFile(\"c:\\testRuby.rb\")");
                 Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "undefined method `Ruby&#39; for IronRuby:Module"));

                
                TestLibrary.WidgetHelper.AddWidgetLayer("Layer039", TestLibrary.Consts.AdminUserName, "This is layer39", " IronRuby.CreateEngine().ExecuteFile(\"c:\\testRuby.rb\") >3");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "undefined method `&gt;&#39; for Microsoft.Scripting.ScopeStorage:Microsoft::Scripting::Hosting::ScriptScope") || selenium.IsTextPresent(validPartMessage));

                TestLibrary.WidgetHelper.AddWidgetLayer("Layer040", TestLibrary.Consts.AdminUserName, "This is layer40", "IronRuby.CreateEngine().ExecuteFile(\"A:\\testIronRuby.rb\") and url('~/default*')");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Could not find a part of the path &#39;A:\testIronRuby.rb&#39")||selenium.IsTextPresent(validPartMessage));

                TestLibrary.WidgetHelper.AddWidgetLayer("Layer040", TestLibrary.Consts.AdminUserName, "This is layer40", "IronRuby.CreateEngine().ExecuteFile(\"testIronRuby.rb\") and url('~/default*')");
                Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Could not find file &#39;c:\\windows\\system32\\inetsrv\\testIronRuby.rb&#39") || selenium.IsTextPresent(validPartMessage));

                 TestLibrary.WidgetHelper.AddWidgetLayer("Layer041", TestLibrary.Consts.AdminUserName, "This is layer41", "IronRuby.CreateEngine().ExecuteFile(\"C:\testRuby.rb\") and true");
                 Assert.IsTrue(selenium.IsTextPresent(validPartMessage + "Illegal characters in path") || selenium.IsTextPresent(validPartMessage));
                
                
                #endregion
            }
            catch (Exception ex)
            {
                errorMessage += ex.ToString();
            }
             finally
             {
                 if (errorMessage.Length > 1)
                     Assert.IsTrue(false, errorMessage);
             }
        }
    }
}
